package net.pulseproject.router.config.instantiator.builder;

import net.pulseproject.router.config.instantiator.builder.validator.FieldValidator;
import net.pulseproject.router.config.instantiator.builder.validator.SkeletonFieldValidator;
import net.pulseproject.router.modules.useable.ValueThresholdModule;


public class ValueThresholdModuleBuilder extends BiDirectionalModuleBuilder<ValueThresholdModule> {

	private Integer lowerThresholdTrigger = null;

	private Integer upperThresholdTrigger = null;

	private Integer startValue = null;

	private Integer endValue = null;

	private Integer transformationTimeInMs = null;

	private Integer fanIn = null;
	
	private final FieldValidator validator = new SkeletonFieldValidatorExtension();
	
	
	public ValueThresholdModuleBuilder() {
		this.addValidator(this.validator);
	}
	
	public final void setLowerThresholdTrigger(final Integer lowerThresholdTrigger) {
		this.lowerThresholdTrigger = lowerThresholdTrigger;
	}

	public final void setUpperThresholdTrigger(final Integer upperThresholdTrigger) {
		this.upperThresholdTrigger = upperThresholdTrigger;
	}

	public final void setStartValue(final Integer startValue) {
		this.startValue = startValue;
	}

	public final void setEndValue(final Integer endValue) {
		this.endValue = endValue;
	}

	public final void setTransformationTimeInMs(final Integer transformationTimeInMs) {
		this.transformationTimeInMs = transformationTimeInMs;
	}

	public final void setFanIn(final Integer fanIn) {
		this.fanIn = fanIn;
	}


	@Override
	protected final ValueThresholdModule buildModule() {
		return new ValueThresholdModule(this.getId(), this.getOutgoingModules(),
				this.lowerThresholdTrigger.intValue(), this.upperThresholdTrigger.intValue(),
				this.startValue.intValue(), this.endValue.intValue(),
				this.transformationTimeInMs.intValue(), this.fanIn.intValue());
	}
	

	private final class SkeletonFieldValidatorExtension extends SkeletonFieldValidator {
		@SuppressWarnings("synthetic-access")
		@Override
		protected void validateFields() {
			if(ValueThresholdModuleBuilder.this.lowerThresholdTrigger == null) {
				throw new RuntimeException("lowerThresholdTrigger not yet set");
			}
			if(ValueThresholdModuleBuilder.this.upperThresholdTrigger == null) {
				throw new RuntimeException("upperThresholdTrigger not yet set");
			}
			if(ValueThresholdModuleBuilder.this.startValue == null) {
				throw new RuntimeException("startValue not yet set");
			}
			if(ValueThresholdModuleBuilder.this.endValue == null) {
				throw new RuntimeException("endValue not yet set");
			}
			if(ValueThresholdModuleBuilder.this.transformationTimeInMs == null) {
				throw new RuntimeException("transformationTimeInMs not yet set");
			}
			if(ValueThresholdModuleBuilder.this.fanIn == null) {
				throw new RuntimeException("fanIn not yet set");
			}
		}
	}

}
